<?php

include_once '../php/metier/SparqlBinding.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SparqlResultset
 *
 * @author peyet
 */
class SparqlResultset {
	private $varNames;
	private $bindings;

	public function __construct(stdClass $response) {
		if (!isset($response->head) && 
				!isset($response->head->vars) &&
				!isset($response->results) &&
				!isset($response->results->bindings)) {
			throw new InvalidArgumentException();
		}
		
		$this->varNames = $response->head->vars;
		$this->bindings = array();
		foreach ($response->results->bindings as $id_row => $row) {
			foreach ($row as $key => $elem) {
				$this->bindings[$id_row][$key] = new SparqlBinding($elem);
			}
		}
	}
	
	public function getVarNames() {
		return $this->varNames;
	}
	
	public function getBindings() {
		return $this->bindings;
	}
	
	public function size() {
		return count($this->bindings);
	}
	
	public function __toString() {
		$string = "Variables names: (\n";
		
		foreach ($this->varNames as $key => $name) {
			$string .= "	[" . $key . "] => " . $name . "\n";
		}
		
		$string .= "),\n"
				. "Bindings: (\n";
		
		foreach ($this->bindings as $id_row => $row) {
			$string .= "	[" . $id_row . "] => (\n";
			
			foreach ($row as $key => $value) {
				$string .= "		[" . $key . "] => " 
						. str_replace("\n", "\n			", $value) . "\n";
			}
			
			$string .= "	)\n";
		}
		
		$string .= ")\n";
		
		return $string;
	}
}
